/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.bean.UserBean;
import com.minierp.dao.AlloBillDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.AlloBill;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

public class AlloBillDaoImpl extends BaseDaoImpl implements AlloBillDao {
	public AlloBill get(String billCode) {
		String sql = "from com.minierp.model.AlloBill as obj where obj.billCode = :billCode and obj.enabled=\'Y\' order by obj.id";
		String[] paramNames = new String[]{"billCode"};
		Object[] values = new Object[]{billCode};
		List list = this.getHibernateTemplate().findByNamedParam(sql,
				paramNames, values);
		return list.isEmpty() ? null : (AlloBill) list.get(0);
	}

	public List getList(String searchKey, boolean examined, long tsId,
			UserBean ub, Date allowStart, int startIndex, int num)
			throws Exception {
		String str = "";
		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (b.managerName like :managerName))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (allowStart != null) {
			str = str + " and b.alloDate>=:allowStart";
		}

		String examineFlag = "N";
		if (examined) {
			examineFlag = "Y";
		}

		String sql = "from com.minierp.model.AlloBill b where b.examined=:examined and b.enabled=\'Y\' "
				+ str + " order by b.id desc";
		Session sess = null;

		List arg14;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("managerName", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setString("examined", examineFlag);
			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg14 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg14;
	}

	public int getListCount(String searchKey, boolean examined, long tsId,
			UserBean ub, Date allowStart) throws Exception {
		String str = "";
		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (b.managerName like :managerName))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (allowStart != null) {
			str = str + " and b.alloDate>=:allowStart";
		}

		String examineFlag = "N";
		if (examined) {
			examineFlag = "Y";
		}

		String sql = "select count(b.id) from com.minierp.model.AlloBill b where b.examined=:examined and b.enabled=\'Y\' "
				+ str;
		Session sess = null;

		int arg13;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("managerName", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setString("examined", examineFlag);
			List list = q.list();
			if (list.isEmpty()) {
				return 0;
			}

			arg13 = Integer.parseInt(list.get(0).toString());
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg13;
	}

	public List getListForCancel(String searchKey, long tsId, UserBean ub,
			Date allowStart, int startIndex, int num) throws Exception {
		String str = "";
		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (b.managerName like :managerName))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (allowStart != null) {
			str = str + " and b.alloDate>=:allowStart";
		}

		String sql = "from com.minierp.model.AlloBill b where b.enabled=\'Y\' "
				+ str + " order by b.id desc";
		Session sess = null;

		List arg12;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("managerName", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg12 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg12;
	}

	public int getListForCancelCount(String searchKey, long tsId, UserBean ub,
			Date allowStart) throws Exception {
		String str = "";
		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (b.managerName like :managerName))";
		}

		if (tsId != 0L) {
			str = str + " and b.tallySetId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tallySetId", ub,
							true);
		}

		if (allowStart != null) {
			str = str + " and b.alloDate>=:allowStart";
		}

		String sql = "select count(b.id) from com.minierp.model.AlloBill b where b.enabled=\'Y\' "
				+ str;
		Session sess = null;

		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("managerName", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			List list = q.list();
			if (!list.isEmpty()) {
				int arg11 = Integer.parseInt(list.get(0).toString());
				return arg11;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return 0;
	}

	public Object[] getStatusValues(long id) {
		String sql = "select b.updateTime,b.enabled,b.examined from com.minierp.model.AlloBill b where b.id="
				+ id;
		List list = this.getHibernateTemplate().find(sql);
		return list.size() > 0 ? (Object[]) list.get(0) : null;
	}
}